import uuid
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django.core.exceptions import ValidationError
from django.urls import reverse

from .models import User

class RegisterForm(UserCreationForm):

    class Meta(UserCreationForm.Meta):
        model = User
        fields = ('nickname', 'email')

    def save(self, *args, **kwargs):
        if not self.instance.username:
            self.instance.username = uuid.uuid1()
        super(RegisterForm, self).save( *args, **kwargs)

    # def clean_email(self):
    #     if '@126.com' in self.cleaned_data['email']:
    #         raise ValidationError('不支持@126邮箱')

class AuthForm(AuthenticationForm):

    def __init__(self,*args, **kwargs):
        super(AuthForm, self ).__init__(*args, **kwargs)
        self.fields['username'].label = '用户名或邮箱'
        url = reverse('users:password_reset')
        self.fields['password'].help_text = f'<a href="{url}">忘记密码</a>'






